package com.punai.customer.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.punai.common.exception.ServiceException;
import com.punai.common.utils.SecurityUtils;
import com.punai.common.utils.StringUtils;
import com.punai.customer.domain.CusExpenseCategories;
import com.punai.customer.domain.CusGxLinkGkCategories;
import com.punai.customer.domain.CusLinkGxfeeCategory;
import com.punai.customer.domain.contants.CusConstants;
import com.punai.customer.domain.vo.CusGxLinkGkCategoriesVo;
import com.punai.customer.domain.vo.CusLinkGkVo;
import com.punai.customer.mapper.CusExpenseCategoriesMapper;
import com.punai.customer.mapper.CusGxFeeMapper;
import com.punai.customer.mapper.CusGxLinkGkCategoriesMapper;
import com.punai.customer.mapper.CusLinkGxfeeCategoryMapper;
import com.punai.customer.service.ICusGxLinkGkCategoriesService;
import com.punai.gk.domain.vo.SimplifyVo;
import com.punai.gk.mapper.GkUseDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 标准费用项下材料类别关联归口Service业务层处理
 *
 * @author zhx
 * @date 2023-03-28
 */
@Service
public class CusGxLinkGkCategoriesServiceImpl extends ServiceImpl<CusGxLinkGkCategoriesMapper,CusGxLinkGkCategories> implements ICusGxLinkGkCategoriesService
{
    @Autowired
    private CusGxLinkGkCategoriesMapper cusGxLinkGkCategoriesMapper;
    @Autowired
    private CusExpenseCategoriesMapper cusExpenseCategoriesMapper;
    @Autowired
    private GkUseDataMapper gkUseDataMapper;
    @Autowired
    private CusGxFeeMapper gxFeeMapper;
    @Autowired
    private CusGxLinkGkServiceImpl cusGxLinkGkService;
    @Autowired
    private CusLinkGxfeeCategoryMapper linkGxfeeCategoryMapper;

    /**
     * 查询标准费用项下材料类别关联归口
     *
     * @param id 标准费用项下材料类别关联归口主键
     * @return 标准费用项下材料类别关联归口
     */
    @Override
    public CusGxLinkGkCategories selectCusGxLinkGkCategoriesById(String id)
    {
        return cusGxLinkGkCategoriesMapper.selectCusGxLinkGkCategoriesById(id, SecurityUtils.getCompanyId());
    }

    /**
     * 查询标准费用项下材料类别关联归口列表
     *
     * @param cusGxLinkGkCategories 标准费用项下材料类别关联归口
     * @return 标准费用项下材料类别关联归口
     */
    @Override
    public List<CusGxLinkGkCategories> selectCusGxLinkGkCategoriesList(CusGxLinkGkCategories cusGxLinkGkCategories)
    {
        return cusGxLinkGkCategoriesMapper.selectCusGxLinkGkCategoriesList(cusGxLinkGkCategories);
    }

    /**
     * 修改标准费用项下材料类别关联归口
     *
     * @param cusLinkGkVo 标准费用项下材料类别关联归口
     * @return 结果
     */
    @Override
    public int updateCusGxLinkGkCategories(Long companyId, CusLinkGkVo cusLinkGkVo)
    {
        String id = cusLinkGkVo.getId();
        String gkType = cusLinkGkVo.getCategory();
        String gkId = cusLinkGkVo.getGkId();
        String specId = cusLinkGkVo.getSpecId();
        String useRange = cusLinkGkVo.getUseRange();
        String featureState = cusLinkGkVo.getFeatureState();
        String gxId = "";
        String categoryId = "";

        if(StringUtils.equals(featureState,"1")) {
            CusGxLinkGkCategories cusGxLinkGkCategories = cusGxLinkGkCategoriesMapper.selectCusGxLinkGkCategoriesById(id, companyId);
            gxId = cusGxLinkGkCategories.getGxId();
            categoryId = cusGxLinkGkCategories.getCategoriesId();
        }else {
            CusLinkGxfeeCategory cusLinkGxfeeCategory = linkGxfeeCategoryMapper.selectDmcLinkGxfeeCategoryById(id, companyId);
            gxId = cusLinkGxfeeCategory.getGxId();
            categoryId = cusLinkGxfeeCategory.getCategoryId();
        }
        if(StringUtils.isEmpty(gxId)){
            throw new ServiceException(CusConstants.GX_NAME + "id获取失败,请联系管理员");
        }
        if (StringUtils.isEmpty(categoryId)) {
            throw new ServiceException("类别id获取失败,请联系管理员");
        }
        if (StringUtils.equals(useRange, CusConstants.USE_RANGE_FEE)) {
            List<String> gxIds = new ArrayList<>(1);
            gxIds.add(gxId);
            // 标准费用项范围
            if (StringUtils.equals(featureState,"1")) {
                cusGxLinkGkCategoriesMapper.updateGkItemByGxId(companyId, gxIds,categoryId, gkType, gkId);
            }
            linkGxfeeCategoryMapper.updateGkItemByGxId(companyId, gxIds,categoryId,  gkType, gkId);
        } else if (StringUtils.equals(useRange, CusConstants.USE_RANGE_FBGC)) {
            // 分部工程
            List<String> gxIds = gxFeeMapper.selectWithFbgc(companyId, gxId);
            // 标准费用项范围
            cusGxLinkGkCategoriesMapper.updateGkItemByGxId(companyId,gxIds,categoryId, gkType,gkId);
            linkGxfeeCategoryMapper.updateGkItemByGxId(companyId,gxIds,categoryId, gkType,gkId);
        } else if (StringUtils.equals(useRange, CusConstants.USE_RANGE_ZY)) {
            // 专业
            cusGxLinkGkCategoriesMapper.updateGkItemBySpecId(companyId,specId,categoryId, gkType,gkId);
            linkGxfeeCategoryMapper.updateGkItemBySpecId(companyId,specId,categoryId, gkType,gkId);
        } else {
            // 没选
            if (StringUtils.equals(featureState, "1")) {
                cusGxLinkGkCategoriesMapper.updateGkItemById(companyId, id, gkType, gkId);
            } else {
                linkGxfeeCategoryMapper.updateGkItemById(companyId, id, gkType, gkId);

            }
        }
        return 200;
    }

    /**
     * 批量删除标准费用项下材料类别关联归口
     *
     * @param ids 需要删除的标准费用项下材料类别关联归口主键
     * @return 结果
     */
    @Override
    public int deleteCusGxLinkGkCategoriesByIds(String[] ids)
    {
        // 没有被调用,安全
        return cusGxLinkGkCategoriesMapper.deleteCusGxLinkGkCategoriesByIds(ids);
    }

    /**
     * 删除标准费用项下材料类别关联归口信息
     *
     * @param id 标准费用项下材料类别关联归口主键
     * @return 结果
     */
    @Override
    public int deleteCusGxLinkGkCategoriesById(String id,Long companyId)
    {
        return cusGxLinkGkCategoriesMapper.deleteCusGxLinkGkCategoriesById(id,companyId);
    }

    @Override
    public List<SimplifyVo> getGkByCategoryId(String categoryId, String gkType, Long companyId) {
        List<SimplifyVo> gkVoList = new ArrayList<>();
        CusExpenseCategories cusExpenseCategories = cusExpenseCategoriesMapper.selectCusExpenseCategoriesById(categoryId, companyId);
        Map<String,List<String>> gkListMap = JSON.parseObject(cusExpenseCategories.getGkItem(), Map.class);
        List<String> gkIds = gkListMap.get(gkType);
        for (String gkId : gkIds) {
            SimplifyVo simplifyVo = new SimplifyVo();
            simplifyVo.setId(gkId);
            if(StringUtils.isNotEmpty(gkId)){
                try {
                    simplifyVo.setName(gkUseDataMapper.selectNameById(gkId,companyId));
                }catch (Exception e){
                    throw new ServiceException("归口字典库丢失");
                }
            }
            gkVoList.add(simplifyVo);
        }
        return gkVoList;
    }

    @Override
    public List<CusGxLinkGkCategoriesVo> getChildrenCategories(String gxLinkGkFeatureId, Long companyId) {
        List<CusGxLinkGkCategoriesVo> cusGxLinkGkCategoriesVos = cusGxLinkGkCategoriesMapper.selectListByGxLinkGkId(gxLinkGkFeatureId, companyId);

        List<SimplifyVo> simplifyVos = gkUseDataMapper.selectIdNameList(companyId);
        Map<String, String> idNameMap = simplifyVos.stream().collect(Collectors.toMap(SimplifyVo::getId, SimplifyVo::getName));

        cusGxLinkGkCategoriesVos.forEach(e -> e.setLinkGkMap(cusGxLinkGkService.addGkName(e.getGkItem(),idNameMap)));
        return cusGxLinkGkCategoriesVos;
    }

    @Override
    public List<SimplifyVo> getHistoryCategories(String specId, String gkType) {
        return cusGxLinkGkCategoriesMapper.getHistoryCategories(SecurityUtils.getCompanyId(), specId, gkType);
    }
}